package lee.vioson.ebook.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.rememberAsyncImagePainter
import com.laiyifen.lyftoast.toast
import lee.vioson.ebook.R
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.utils.BookJumper
import lee.vioson.ebook.utils.ordinal
import lee.vioson.remotedata.bean.BookData
import lee.vioson.remotedata.bean.Chapter
import lee.vioson.remotedata.bean.boolean

@Composable
fun ChapterItem(
    chapter: Chapter, book: BookData?, navHostController: NavHostController? = null,
    onItemClick: (() -> Unit)? = null
) {
    val context = LocalContext.current
    Row(
        Modifier
            .fillMaxWidth()
            .clickable {
                if (navHostController != null) {
                    if (chapter.isVip.boolean) {
                        navHostController.navigate(AppDestinations.VIP_SCREEN)
                    } else {
                        if (book != null) {
                            BookJumper.INSTANCE.jumpToChapterDetail(
                                chapter,
                                book,
                                navHostController
                            )
                        } else {
                            context.toast(context.getString(R.string.Book_data_is_null))
                        }
                    }
                } else {
                    onItemClick?.invoke()
                }
            }
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            painter = rememberAsyncImagePainter(model = chapter.coverPhoto),
            contentDescription = "photo of article chapter",
            modifier = Modifier.size(64.dp),
            contentScale = ContentScale.Crop
        )
        Column(
            Modifier
                .fillMaxHeight()
                .weight(1f)
                .padding(horizontal = 12.dp),
            verticalArrangement = Arrangement.SpaceAround
        ) {
            Text(
                text = chapter.chapterName,
                fontSize = 14.sp,
                color = AppTheme.appColors.grayTextColor
            )
            Spacer(modifier = Modifier.height(15.dp))
            Text(
                text = stringResource(id = R.string.chapter_number, chapter.chapterNum.ordinal),
                fontSize = 10.sp,
                color = AppTheme.appColors.grayTextColor
            )
        }
        if (chapter.isVip.boolean) {
            Button(
                shape = RoundedCornerShape(4.dp),
                colors = AppTheme.vipButtonColors(),
                onClick = {
                    //to sub vip
                }) {
                Text(
                    text = stringResource(id = R.string.VIP),
                    fontSize = 14.sp,
                    color = AppTheme.appColors.blackTextColor
                )
            }
        } else {
            Box(modifier = Modifier.padding(ButtonDefaults.ContentPadding)) {
                Text(text = stringResource(id = R.string.Free))
            }
        }
    }
}
